/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.report.html;

import org.open.cobertura.data.ICoberturaPackageCovered;
import org.open.cobertura.enums.DataType;
import org.open.cobertura.model.*;
import org.open.cobertura.report.ICreateReport;
import org.open.cobertura.report.html.provider.ReportCoverageProvider;
import org.open.cobertura.report.html.provider.SourceHighLight;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class CreateHtmlReport extends AbstractCreateReport implements ICreateReport {
    private final static Logger logger = Logger.getLogger(CreateHtmlReport.class.getName());
    @Override
    public void createReport(List<ICoberturaPackageCovered> coberturaPackageCovered,final String sourceFile) {
        readSourceAll(sourceFile);
        IReportCoverage packageCoverage=new ReportCoverageProvider();
        ReportCoverage reportCoverage =packageCoverage.providePackageCoverage(coberturaPackageCovered);
        Map<String,Object> reportMap=new HashMap<>();
        reportMap.put(DataType.PACKAGE.name(),reportCoverage.getPackageCoverageList());
        reportMap.put(DataType.CLASS.name(), reportCoverage.getClassCoverageMap().get(reportCoverage.getPackageCoverageList().get(0).getPackageName()));
        reportMap.put(DataType.REPORTPATH.name(), "./");
        outputHtml(reportMap,"index.html",DataType.PACKAGE);
        outputClassCoverageHtml(reportCoverage.getPackageCoverageList(),reportCoverage.getClassCoverageMap());
        outputMethodCoverageHtml(reportCoverage.getPackageCoverageList(),reportCoverage.getMethodCoverageMap());
        outputSourceCoverageHtml(reportCoverage.getLineCoverageMap());
        logger.info("生成报告完成");
    }

    @Override
    public void outputClassCoverageHtml(List<PackageCoverage> packageCoverageList,Map<String, List<ClassCoverage>> classCoverageMap) {
        for(Map.Entry<String,List<ClassCoverage>> entry:classCoverageMap.entrySet()){
            Map<String,Object> classMap=new HashMap<>();
            classMap.put(DataType.PACKAGE.name(),packageCoverageList);
            classMap.put(DataType.CLASS.name(),entry.getValue());
            classMap.put(DataType.REPORTPATH.name(), "../");
            classMap.put(DataType.NAV.name(), entry.getKey());
            createPackageDir(entry.getKey());
            outputHtml(classMap,entry.getKey()+"/index.html",DataType.CLASS);
        }
    }

    @Override
    public void outputMethodCoverageHtml(List<PackageCoverage> packageCoverageList,Map<String, List<MethodCoverage>> methodCoverageMap) {
        for(Map.Entry<String,List<MethodCoverage>> entry:methodCoverageMap.entrySet()){
            String className=entry.getKey();
            if(entry.getKey().indexOf("{...}")!=-1){
                className=className.substring(0,entry.getKey().replaceAll("\\{...}","").lastIndexOf("."));
            }
            Map<String,Object> methodMap=new HashMap<>();
            methodMap.put(DataType.PACKAGE.name(),packageCoverageList);
            methodMap.put(DataType.METHOD.name(),entry.getValue());
            methodMap.put(DataType.REPORTPATH.name(), "../");
            methodMap.put(DataType.NAV.name(), getPackageName(className));
            outputHtml(methodMap,getPackageName(className)+"/"+getClassName(className)+".html",DataType.METHOD);
        }
    }

    @Override
    public void outputSourceCoverageHtml(Map<String, List<LineCoverage>> lineCoverageMap) {
        for(Map.Entry<String,List<LineCoverage>> entry:lineCoverageMap.entrySet()){
            String className=entry.getKey();
            if(entry.getKey().indexOf("{...}")!=-1){
                className=className.substring(0,entry.getKey().replaceAll("\\{...}","").lastIndexOf("."));
            }
            Map<String,Object> sourceMap=new HashMap<>();
            sourceMap.put(DataType.REPORTPATH.name(), "../");
            List<String> source=getSourceList(className);
            ISourceHighLight sourceHighLight=new SourceHighLight();
            sourceHighLight.highLight(entry.getValue(),source);
            sourceMap.put(DataType.SOURCE.name(),getSource(className));
            sourceMap.put(DataType.NAVPACKAGE.name(),getPackageName(className));
            sourceMap.put(DataType.NAV.name(),getClassName(className));
            outputHtml(sourceMap,getPackageName(className)+"/"+getClassName(className)+"-source.html",DataType.SOURCE);
        }
    }
}
